import Vue from "vue"
import VueRouter from "vue-router"
import Home from "../views/Home"
import Search from "../views/Search"
import Login from "../views/Login"
import Register from "../views/Register"

Vue.use(VueRouter)

// 重写
const originPush = VueRouter.prototype.push
const originReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function (location, okCallback, errCallback) {
  if (!okCallback && !errCallback) {
    return originPush.call(this, location).catch(() => {})
  }
  return originPush.call(this, location, okCallback, errCallback)
}

VueRouter.prototype.replace = function (location, okCallback, errCallback) {
  if (!okCallback && !errCallback) {
    return originReplace.call(this, location).catch(() => {})
  }
  return originReplace.call(this, location, okCallback, errCallback)
}

const router = new VueRouter({
  routes: [
    { path: "/", redirect: "/home" },
    {
      path: "/home",
      name: "home",
      component: Home,
      meta: {
        isShowSort: true,
      },
    },
    { path: "/search/:keyword?", name: "search", component: Search },
    {
      path: "/login",
      name: "login",
      component: Login,
      meta: {
        isShowFooter: true,
      },
    },
    {
      path: "/register",
      name: "register",
      component: Register,
      meta: {
        isShowFooter: true,
      },
    },
  ],
})

export default router
